<?php
/**
 * Created by PhpStorm.
 * User: gqn
 * Date: 2016/3/8
 * Time: 16:12
 */

namespace Api\Controller;

class SkillConfigController extends BaseController{

    static public $treelist = array();

    public static function tree($data,$pid=0,$level=0){
        foreach($data as $key=>$value){
            if($value['pid']==$pid){
                $value['level'] = $level;
                self::$treelist[] = $value;
                unset($data[$key]);
                self::tree($data,$value['category_id'],$level+1);
            }
        }
        return self::$treelist;
    }

    public function index(){
        //解决跨域
        echoHeader();
        $user_id = I('post.user_id');
        $data = self::checkUserIsExist($user_id);
        if(!$data['user']){
            $this->ajaxReturn($data);
        }
        $expsql = "select conf.category_id,cat.category_name,cat.pid from zsf_user_repair_conf as conf left join zsf_category as cat on cat.category_id = conf.category_id where conf.is_good =1 and conf.user_id = $user_id and cat.pid!=0";
        $expert= M()->query($expsql);
        $stasql = "select conf.category_id,cat.category_name,cat.pid from zsf_user_repair_conf as conf left join zsf_category as cat on cat.category_id = conf.category_id where conf.is_good =0 and conf.user_id = $user_id and cat.pid!=0";
        $standard= M()->query($stasql);
        if(empty($expert) && empty($standard)){
            $info = array("err_code"=>1009,"err_msg"=>"暂无数据");
            $this->ajaxReturn($info);
        }
        $data['standard'] = $standard;
        $data['expert'] = $expert;
        $data['standard']['count'] = count($data['standard']);
        $data['expert']['count'] = count($data['expert']);
        $info = array("err_code"=>200,"err_msg"=>$data);
        $this->ajaxReturn($info);  
    }

    public function entry(){
        //解决跨域
        echoHeader();
        $user_id = I('post.user_id');
        $data = self::checkUserIsExist($user_id);
        if(!$data['user']){
            $this->ajaxReturn($data);
        }
        $is_good = I('post.is_good');
        if($is_good==''){
            $info = array("err_code"=>404,"err_msg"=>'参数缺失');
            $this->ajaxReturn($info);
        }
        $data = M('category')->field('category_name,category_id,pid')->order(array('order'=>'desc'))->select();
        if(empty($data)){
            $info = array("err_code"=>1009,"err_msg"=>"暂无数据");
            $this->ajaxReturn($info);
        }
        $tree = self::tree($data);
        $catid = M('user_repair_conf')->field('category_id')->where("user_id = $user_id and is_good = $is_good")->select();
        if(!empty($catid)){
            foreach($tree as $i=>$v){
                $tree[$i]['select'] = null;
                   foreach($catid as $j=>$d) {
                    if($tree[$i]['category_id']==$catid[$j]['category_id']){
                        $tree[$i]['select'] = "selected";
                    }
                }
            }
        }
        $info = array("err_code"=>200,"err_msg"=>$tree);
        $this->ajaxReturn($info);
    }

    public function save(){
        //解决跨域
        echoHeader();
        $catego_id = I('post.category_id','');
        $category_id = explode(',',$catego_id);
        $user_id = I('post.user_id');
        $data = self::checkUserIsExist($user_id);
        if(!$data['user']){
            $this->ajaxReturn($data);
        }
        $is_good = I('post.is_good','');
        if($is_good==''||$category_id==''){
            $info = array("err_code"=>404,"err_msg"=>'参数缺失');
            $this->ajaxReturn($info);
        }
        $catid = M('user_repair_conf')->field('category_id')->where("user_id = $user_id and is_good = $is_good")->select();
        if($catid){
            M('user_repair_conf')->field('category_id')->where("user_id = $user_id and is_good = $is_good")->delete();
        }
        foreach($category_id as $i=>$v){
            $info['user_id'] = $user_id;
            $info['category_id'] = $category_id[$i];
            $info['is_good'] = $is_good;
            M('user_repair_conf')->add($info);
        }
        session('repair_config',$catego_id);
        $info = array("err_code" => 200, "err_msg" => "保存成功");
        $this->ajaxReturn($info);

    }

    public function del(){
        //解决跨域
        echoHeader();
        $user_id = I('post.user_id');
        $is_good = I('post.is_good'); //0普通 1专长
        $data = self::checkUserIsExist($user_id);
        if(!$data['user']){
            $this->ajaxReturn($data);
        }
        $category_id = I('post.category_id');

        $map['user_id'] = array('eq',$user_id);
        $map['category_id'] = array('eq',$category_id);
        $map['is_good'] = array('eq',$is_good);
        $data = M('user_repair_conf')->where($map)->delete();

        if($data){
            $info = array("err_code"=>200,"err_msg"=>"删除成功");
            $this->ajaxReturn($info);
        }else{
            $info = array("err_code"=>403,"err_msg"=>"删除失败");
            $this->ajaxReturn($info);
        }

    }
}